from sqlalchemy import create_engine
import tushare as ts
from baseStock import BaseStock
from baseEngine import  engine
import time
import json

class SetAll:
    def setHs300():
        #读取沪深300的股票，并保存到数据库
        hs300Result = BaseStock.getHs300()
        nowDate = time.strftime('%Y-%m-%d',time.localtime(time.time()))
        startDate = '2013-01-01'
        # print(hs300Result)
        for row in hs300Result:
            tableName = 'hs300_details'
            path = '/home/tike/python_test/tushare/stock_num/'
            oneResult = ts.get_hist_data(row[1], start=startDate, end=nowDate)
            oneResult.to_json(path + row[1] + '.json', orient='records')
            # oneResult.to_sql(tableName, engine.getEngine(), if_exists='append')
        print("已截取沪深300的 %s 到 %s的每日股价数据"%(startDate, nowDate))

    def setZz500():
        zz500Result = BaseStock.getZz500()
        nowDate = time.strftime('%Y-%m-%d', time.localtime(time.time()))
        startDate = '2010-01-01'
        for row in zz500Result:
            path = '/home/tike/python_test/tushare/stock_num/'
            oneResult = ts.get_hist_data(row[1], start=startDate, end=nowDate)
            oneResult.to_json(path + row[1] + '.json', orient='records')
            
    def setHighBalance():
        
        return ''

selNum = int(input("请输入：\n 1.更新沪深300日期股价。\n 2.更新中证500日期股价.\n 3.更新沪深300盈利能力最高的股票\n"))
if selNum == 1:
    SetAll.setHs300()
elif selNum == 2:
    SetAll.setZz500()
elif selNum == 3:
    SetAll.setHighBalance()